﻿using System.Collections.Generic;
using Glue;
using Xunit;

namespace GlueTests.MappingTests
{
    public class When_adding_a_OneDirection_relation_between_lists_of_nested_types
    {
        private readonly Mapping<DomainPerson, GuiPerson> mapping;

        public class DomainPerson { public List<DomainAddress> Addresses { get; set; } }
        public class DomainAddress { }

        public class GuiPerson { public List<GuiAddress> Addresses { get; set; } }
        public class GuiAddress { }

        public When_adding_a_OneDirection_relation_between_lists_of_nested_types()
        {
            mapping = new Mapping<DomainPerson, GuiPerson>();
        }

        [Fact]
        public void Should_throw_exception_when_adding_TowardsLeft_but_converter_does_not_have_creator_TowardsLeft()
        {
            mapping.AddConverter(CreateMappingWithOnlyTowardsRightCreator());
            Assert.Throws<GlueException>(() =>
                    mapping.RelateTowardsLeft(domain => domain.Addresses, gui => gui.Addresses)
                );
        }

        [Fact]
        public void Should_accept_when_adding_TowardsLeft_but_if_TowardsLeft_creator_exists()
        {
            mapping.AddConverter(CreateMappingWithOnlyTowardsLeftCreator());
            mapping.RelateTowardsLeft(domain => domain.Addresses, gui => gui.Addresses);
        }

        [Fact]
        public void Should_throw_exception_when_adding_TowardsRight_but_converter_does_not_have_creator_TowardsRight()
        {
            mapping.AddConverter(CreateMappingWithOnlyTowardsLeftCreator());
            Assert.Throws<GlueException>(() =>
                    mapping.RelateTowardsRight(domain => domain.Addresses, gui => gui.Addresses)
                );
        }

        [Fact]
        public void Should_accept_when_adding_TowardsRight_but_if_TowardsRight_creator_exists()
        {
            mapping.AddConverter(CreateMappingWithOnlyTowardsRightCreator());
            mapping.RelateTowardsRight(domain => domain.Addresses, gui => gui.Addresses);
        }



        private Mapping<DomainAddress, GuiAddress> CreateMappingWithOnlyTowardsRightCreator()
        {
            return new Mapping<DomainAddress, GuiAddress>(null, x=>new GuiAddress());
        }

        private Mapping<DomainAddress, GuiAddress> CreateMappingWithOnlyTowardsLeftCreator()
        {
            return new Mapping<DomainAddress, GuiAddress>(x=>new DomainAddress(), null);
        }
    }
}
